<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="950" @closed="$emit('closed')" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
		<el-alert title="首次登录请修改密码。密码更新成功后，您将被重定向到登录页面，您可以使用新密码重新登录。" type="info" show-icon style="margin-bottom: 15px;"/>
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm"
				 label-width="110px"
				 label-position="left">
			<el-form-item label="当前密码" prop="oldPassword">
				<el-input v-model="form.oldPassword" type="password" show-password placeholder="请输入当前密码"></el-input>
				<div class="el-form-item-msg">必须提供当前登录用户密码才能进行更改</div>
			</el-form-item>
			<el-form-item label="新密码" prop="password">
				<el-input v-model="form.password" type="password" show-password placeholder="请输入新密码"></el-input>
				<sc-password-strength v-model="form.password"></sc-password-strength>
				<div class="el-form-item-msg">请输入包含英文、数字的8位以上密码</div>
			</el-form-item>
			<el-form-item label="确认新密码" prop="confirmNewPassword">
				<el-input v-model="form.confirmNewPassword" type="password" show-password placeholder="请再次输入新密码"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">修改密码</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import scPasswordStrength from '@/components/scPasswordStrength'

	export default {
		emits: ['success', 'closed'],
		components: {
			scPasswordStrength
		},
		data() {
			return {
				mode: "editpassword",
				titleMap: {
					editpassword: '修改密码',
				},
				visible: false,
				isSaveing: false,
				form: {
					'':'',
					oldPassword: "",
					password: "",
					confirmNewPassword: ""
				},
				rules: {
					oldPassword: [
						{required: true, message: '请输入当前密码'}
					],
					password: [
						{required: true, message: '请输入新密码'}
					],
					confirmNewPassword: [
						{required: true, message: '请再次输入新密码'},
						{
							validator: (rule, value, callback) => {
								if (value !== this.form.password) {
									callback(new Error('两次输入密码不一致'));
								} else {
									callback();
								}
							}
						}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode = 'editpassword') {
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						this.$API['admin.user'].rePassword.post(this.form)
							.then((res) => {
								if (res.code == 0) {
									this.visible = false;
									this.$message.success("操作成功")
									this.$router.replace({path: '/login'});
								} else {
									this.$alert(res.msg, "提示", {type: 'error'})
								}
							}).catch((msg) => {
							this.$alert(msg, "提示", {type: 'error'})
						}).finally(() => {
							this.isSaveing = false;
						});
					} else {
						return false;
					}
				})
			},
		}
	}
</script>

<style>
	.el-overlay {
		//background-color:#f6f8f9;
	}
</style>
